package com.shop.mms_client.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.shop.mms_client.R;
import com.shop.mms_client.model.Message;
import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
    private List<Message> messages;

    public MessageAdapter(List<Message> messages) {
        this.messages = messages;
    }

    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_message, parent, false);
        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MessageViewHolder holder, int position) {
        Message message = messages.get(position);
        holder.bind(message);
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    static class MessageViewHolder extends RecyclerView.ViewHolder {
        TextView senderText;
        TextView contentText;
        ImageView messageImage;

        MessageViewHolder(View itemView) {
            super(itemView);
            senderText = itemView.findViewById(R.id.senderText);
            contentText = itemView.findViewById(R.id.contentText);
            messageImage = itemView.findViewById(R.id.messageImage);
        }

        void bind(Message message) {
            senderText.setText(message.getSender());
            contentText.setText(message.getContent());
            
            if (message.getImageUrl() != null && !message.getImageUrl().isEmpty()) {
                messageImage.setVisibility(View.VISIBLE);
                Glide.with(messageImage.getContext())
                    .load(message.getImageUrl())
                    .into(messageImage);
            } else {
                messageImage.setVisibility(View.GONE);
            }
        }
    }
} 